#include "type.h"
#include "assert.h"
#include "color.h"
#include "graph.h"
#include "lcd.h"
#include "mem.h"
#include "-gui.h"
#include "button.h"

void button_draw(const struct window *win)
{
	color_t rgb1, rgb3;
	struct rect rt;
	struct button *bt;
	bt = win->info;
	rt = win->rect;
	if (win->rect.start_pt.x > win->rect.end_pt.x) {
		rt.start_pt.x = win->rect.end_pt.x;
		rt.end_pt.x = win->rect.start_pt.x;
	}
	if (win->rect.start_pt.y > win->rect.end_pt.y) {
		rt.start_pt.y = win->rect.end_pt.y;
		rt.end_pt.y = win->rect.start_pt.y;
	}
	if (bt->status == STATUS_INACTIVE) {
		rgb1 = RGB(255, 255, 255);
		rgb3 = RGB(0xd4, 0xd0, 0xc8);
	} else {
		rgb1 = RGB(64, 64, 64);
		rgb3 = RGB(0xd4, 0xd0, 0xc8);
	}
	if ((bt->style & BUTTON_STYLE_BORDER) == BUTTON_STYLE_BORDER)
		draw_button_rect(&rt, bt->status, DRAW_FILL);
	else
		lcd_draw_rect(&rt, DEFAULT_WIN_COLOR, DRAW_FILL);
	if (bt->caption) {
		rgb1 = lcd_set_bgcolor(rgb3);
		lcd_rect_str(&rt, bt->caption, RECT_STR_CENTER);
		lcd_set_bgcolor(rgb1);
	}
}

int button_create(struct window *win, const char *caption,
	          void (*draw)(const struct window *),
                  void (*func)(const struct window *))
{
	win->info = (struct button *)mem_alloc(sizeof(struct button));
	if (!win->info) {
		return 0;
	}
	((struct button *)win->info)->caption = caption;
	((struct button *)win->info)->status = STATUS_INACTIVE;
	((struct button *)win->info)->style = BUTTON_STYLE_BORDER;
	if (!draw)
		win->draw = button_draw;
	else
		win->draw = draw;
	return 1;
}

